package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.ZhongBaoMessage;
import org.itboys.mobile.service.mongo.weixin.ZhongBaoWeiXinMessageService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：江雄
 * 时间：2017年5月9日
 * 描述：众包_微信_消息_controller
 */
@Controller
@RequestMapping("/zhongbao/weixin/message")
public class ZhongBaoWeiXinMessageController extends BaseController{
    @Autowired
    private ZhongBaoWeiXinMessageService messageService;
    /**
     * 设置已读
     * @param request
     * @param response
     * @param
     */
    @RequestMapping(value="/read", method=RequestMethod.POST)
    @ApiOperation(value="处理消息（已读/未读）")
    public void read(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required=false, name="messageId", value="消息ID")@RequestParam(value = "messageId", required = false)Long messageId,
            @ApiParam(required=false, name="messageIds", value="消息ID集合")@RequestParam(value = "messageIds", required = false)List<Long> messageIds
    ){
        try {
           // SundayMemberSessionDto memberSessionDto = AccountIdHoldUtils.getSessionMember(request);
            messageService.read(messageId,messageIds);
            ResponseMessage.success();
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.info("/zhongbao/weixin/message/read--------"+e);
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    @RequestMapping(value="/index", method= RequestMethod.GET)
    @ApiOperation(value="消息列表页。")
    public String index(
            HttpServletRequest request,
            HttpServletResponse response,
          //  @ApiParam(required=true, name="type", value="1，项目审核类，2报名审核类，3留言类")@RequestParam(value = "type", required = true)Integer type,
            @ApiParam(required=false, name="status", value="0未读1已读,默认不传")@RequestParam(value = "status", required = false)Integer status,

            Model model
    ){
        try{
          // Map<String,Object> result= messageService.getIndexData(AccountIdHoldUtils.getSessionMember(request).getId());
            model.addAttribute("status",status);
          //  model.addAttribute("type",type);
            return "/zhongbao/weixin/message/index";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/zhongbao/weixin/message/index----e="+e.getMessage());

            }
            e.printStackTrace();
            return "/zhongbao/weixin/error";
        }
    }
    @RequestMapping(value="/getData", method= RequestMethod.POST)
    @ApiOperation(value="消息数据页")
    public String getData(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model,
           /* @ApiParam(required=true, name="type", value="1，项目审核类，2报名审核类，3留言类")@RequestParam(value = "type", required = true)Integer type,
           */ @ApiParam(required=false, name="status", value="0未读1已读,默认不传")@RequestParam(value = "status", required = false)Integer status,

            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order

            ){
        try{

            List<ZhongBaoMessage> messages = messageService.getData(request,AccountIdHoldUtils.getSessionMember(request).getId(),status);
              model.addAttribute("messages",messages);
            return "/zhongbao/weixin/message/data";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/zhongbao/weixin/message/getData----e="+e.getMessage());

            }
            e.printStackTrace();
            return "/zhongbao/weixin/error";
        }
    }

}
